package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.system.domain.MiniProxy;
import com.ruoyi.system.domain.MiniUser;
import com.ruoyi.system.domain.vo.HomeStatisticsVO;
import com.ruoyi.system.domain.vo.h5.IncomeSummaryVO;
import com.ruoyi.system.service.IMiniProxyService;
import com.ruoyi.system.service.MiniUserService;
import com.ruoyi.system.mapper.MiniUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
    * @author 李鼎
* @description 针对表【mini_user(小程序用户表)】的数据库操作Service实现
* @createDate 2023-11-09 16:13:36
*/
@Service
public class MiniUserServiceImpl extends ServiceImpl<MiniUserMapper, MiniUser>
    implements MiniUserService{
    @Autowired
    private IMiniProxyService miniProxyService;

    @Override
    public MiniUser createUser(String openid, String inviteCode) {
        return createUserInit(openid,inviteCode);
    }

    @Override
    public IncomeSummaryVO selectInviteCountByOpenid(String openid) {
        MiniUser user = this.getOne(Wrappers.<MiniUser>lambdaQuery().eq(MiniUser::getOpenid, openid));
        return this.getBaseMapper().selectInviteCountByInviteUserId(user.getId());
    }

    @Override
    public List<IncomeSummaryVO> selectInviteRecordByOpenid(String openid) {
        MiniUser user = this.getOne(Wrappers.<MiniUser>lambdaQuery().eq(MiniUser::getOpenid, openid));
        return this.getBaseMapper().selectInviteRecordByInviteUserId(user.getId());
    }

    @Override
    public MiniUser getMyInfo(String openid) {
        MiniUser user = this.getOne(Wrappers.<MiniUser>lambdaQuery().eq(MiniUser::getOpenid, openid));
        if (Objects.nonNull(user)){
            if("88".equals(user.getVip())){
                if (user.getVipTime()!=null){
                    LocalDateTime now = LocalDateTime.now();
                    long vipRemain = ChronoUnit.DAYS.between(now, user.getVipTime().toLocalDateTime());
                    if (vipRemain<=0){
                        user.setVip("0");
                        this.update(Wrappers.<MiniUser>lambdaUpdate().set(MiniUser::getVipFlag,0)
                                .eq(MiniUser::getId,user.getId()));

                    }
                }
            }
        }
        return user;
    }

    public MiniUser createUserInit(String openid,String inviteCode){
        MiniUser miniUser = new MiniUser();
        miniUser.setOpenid(openid);
        miniUser.setNickname("微信用户"+ UUID.randomUUID().toString().substring(0,8));
        if (inviteCode.startsWith(UserConstants.PROXY_INVITE_CODE_PREFIX)) {
            MiniProxy proxy = new MiniProxy();
            proxy.setShareKey(inviteCode);
            List<MiniProxy> miniProxyList = miniProxyService.selectMiniProxyList(proxy);
            if (!CollectionUtils.isEmpty(miniProxyList)) {
                miniUser.setInviteUserId(miniProxyList.get(0).getId());
            }
            miniUser.setInviteType("p");
        }
        miniUser.setInviteCode(UserConstants.USER_INVITE_CODE_PREFIX+openid.substring(0,8));
        miniUser.setCreateTime(Timestamp.valueOf(LocalDateTime.now()));
        this.save(miniUser);
        return miniUser;
    }
}




